package com.his.modules.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.core.entity.R;
import com.his.core.utils.StringUtils;
import com.his.modules.business.entity.*;
import com.his.modules.business.enums.BlflEnum;
import com.his.modules.business.service.MedicalRecordService;
import com.his.modules.business.service.MsBrdaService;
import com.his.modules.business.service.OmrBl01Service;
import com.his.modules.business.utils.MedicalRecordAnalysisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@Slf4j
public class OmrBl01Controller {
    @Autowired
    private OmrBl01Service omrBl01Service;
    @Autowired
    private MsBrdaService msBrdaService;
    @Autowired
    private MedicalRecordService medicalRecordService;

    /**
     * 门诊病历列表查询
     * @param page
     * @param omrBl01
     * @return
     */
    @GetMapping("mzList")
    public ModelAndView mzList(Page page, OmrBl01 omrBl01) {
        QueryWrapper<OmrBl01> queryWrapper = new QueryWrapper<>();
        //新病历
        queryWrapper.eq("blsylx", "2");
        //已完成
        queryWrapper.eq("blzt", "1");
        if(null != omrBl01.getBlbh()) {
            queryWrapper.eq("blbh", omrBl01.getBlbh());
        }
        if(StringUtils.isNotBlank(omrBl01.getBlmc())) {
            queryWrapper.like("blmc", omrBl01.getBlmc());
        }
        if(null != omrBl01.getJzxh()) {
            queryWrapper.eq("jzxh", omrBl01.getJzxh());
        }
        ModelAndView modelAndView = new ModelAndView();
        Page<OmrBl01> omrBl01Page = omrBl01Service.page(page, queryWrapper);
        if(!CollectionUtils.isEmpty(omrBl01Page.getRecords())) {
            for(OmrBl01 omrBl011 : omrBl01Page.getRecords()) {
                Long brid = omrBl011.getBrid();
                MsBrda msBrda = msBrdaService.getById(brid);
                omrBl011.setBrxm(null == msBrda?"":msBrda.getBrxm());
            }
        }
        modelAndView.addObject("omrBl01", omrBl01);
        modelAndView.addObject("page", omrBl01Page);
        modelAndView.setViewName("mzList");
        return modelAndView;
    }

    /**
     * 门诊病历解析
     * @param omrBl01
     * @return
     */
    @ResponseBody
    @PostMapping("mzAnalysis")
    public R mzAnalysis(@RequestBody OmrBl01 omrBl01) {
        if(StringUtils.isBlank(omrBl01.getBlbhs())) {
            return R.failed("请选择病历！");
        }
        log.info("解析门诊病历=================");
        MedicalRecord medicalRecord = new MedicalRecord();
        medicalRecord.setBlfl(BlflEnum.MZ.getCode());
        medicalRecord.setBlbhs(omrBl01.getBlbhs());
        List<MedicalRecord> medicalRecordList = medicalRecordService.medicalRrdList(medicalRecord);
        String blbhs = "";

        if(!CollectionUtils.isEmpty(medicalRecordList)) {
            System.out.println("解析门诊病历开始执行=================获取病历数：" + medicalRecordList.size());
            for(MedicalRecord mr : medicalRecordList) {
                try {
                    String blnr = new String(mr.getBlnr(), "GBK");
                    MedicalRecordAnalysis medicalRecordAnalysis = new MedicalRecordAnalysis();
                    medicalRecordAnalysis.setBlbh(mr.getBlbh());
                    medicalRecordAnalysis.setBlfl(mr.getBlfl());
                    medicalRecordAnalysis.setBlmc(mr.getBlmc());
                    medicalRecordAnalysis.setJxzt("1");
                    List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList = MedicalRecordAnalysisUtil.medicalRecordAnalysis(blnr, medicalRecordAnalysis);
                    if(!CollectionUtils.isEmpty(medicalRecordAnalysisRecordList)) {
                        for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                            medicalRecordAnalysisRecord.setBlbh(mr.getBlbh());
                            medicalRecordAnalysisRecord.setBllx(mr.getBllx());
                            medicalRecordAnalysisRecord.setBllb(mr.getBllb());
                            medicalRecordAnalysisRecord.setMblb(mr.getMblb());
                            medicalRecordAnalysisRecord.setMbbh(mr.getMbbh());
                            medicalRecordAnalysisRecord.setJzxh(mr.getJzxh());
                        }
                    }
                    //保存解决数据
                    medicalRecordService.medicalRecordAnalysis(medicalRecordAnalysis, medicalRecordAnalysisRecordList);
                } catch (UnsupportedEncodingException e) {
                    log.info("解析出错了！" + e.getMessage());
                    blbhs += mr.getBlbh() + ";";
                }
            }
        }
        if(StringUtils.isBlank(blbhs)) {
            return R.ok(null, "病历解析成功！");
        }
        return R.failed("病历编号为：" + blbhs + "的病历，解析过程中出现异常，请重新解析或排查异常！");
    }
}